<template>
  <div>
    <div class="history" v-if="ifShowHistory">
      <div class="top">
        <h4>历史记录</h4>
        <van-icon name="delete-o" @click="delHistory" />
      </div>
      <div class="down">
        <van-tag
          plain
          type="default"
          @click="tagClick(item)"
          v-for="(item, index) in historyKeywordList"
          :key="index"
          >{{ item }}</van-tag
        >
      </div>
    </div>
    <div class="history">
      <div class="top">
        <h4>热门搜索</h4>
      </div>
      <div class="down">
        <van-tag
          plain
          @click="tagClick(item.keyword)"
          :type="item.is_hot == 1 ? 'danger' : 'default'"
          v-for="(item, index) in hotKeywordList"
          :key="index"
          >{{ item.keyword }}</van-tag
        >
      </div>
    </div>
  </div>
</template>

<script>
import { GetHistoryHotApi, DelHistoryAPI } from '../request/api'
export default {
  name: 'historyhot',
  data () {
    return {
      // 历史数组
      historyKeywordList: [],
      // 热门数组
      hotKeywordList: [],
      // 是否显示history区块
      ifShowHistory: true
    }
  },
  created () {
    GetHistoryHotApi().then(res => {
      console.log(res)
      if (res.errno == 0) {
        let { defaultKeyword, historyKeywordList, hotKeywordList } = res.data
        this.historyKeywordList = historyKeywordList
        this.hotKeywordList = hotKeywordList
        this.$emit('changePlaceholder', defaultKeyword.keyword)
      }
    })
  },
  methods: {
    tagClick (val) {
      // console.log(val)
      this.$emit('tagClick', val)
    },
    // 清除历史记录
    delHistory () {
      DelHistoryAPI().then(res => {
        if (res.errno == 0) {
          this.$toast.success('删除成功')
          setTimeout(() => {
            this.ifShowHistory = false
          }, 1000)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.history {
  padding: 0 2%;
  background: #fff;
  margin-bottom: 0.2rem;
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 0.5rem;
    font-size: 0.2rem;
    h4 {
      font-weight: normal;
    }
  }
  .down {
    .van-tag {
      font-size: 0.14rem;
      margin-right: 10px;
      margin-bottom: 10px;
      padding: 3px;
    }
  }
}
</style>
